package conceptmaniax.hotelapp.activities;

import java.text.ParseException;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import conceptmaniax.hotelapp.config.Constants;
import conceptmaniax.hotelapp.enums.Language;
import conceptmaniax.hotelapp.managers.RequestManager;

public class LanguageSelectionActivity extends Activity {
	protected TextView counter;
	boolean firstrun;
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.language_selection);

		final SharedPreferences settings = getSharedPreferences(Constants.APP_NAME, 0);
		final SharedPreferences.Editor editor = settings.edit();
		firstrun = settings.getBoolean("LanguageFirstRun", true);
		final Button requests = (Button) findViewById(R.id.requestsButton);
		final Button go_to_home = (Button) findViewById(R.id.homeButton);
		counter = (TextView) findViewById(R.id.counter);

		if (firstrun) {
			editor.putBoolean("LanguageFirstRun", false);
			editor.commit();
			requests.setVisibility(View.INVISIBLE);
			go_to_home.setVisibility(View.INVISIBLE);
			counter.setVisibility(View.INVISIBLE);
		}

		go_to_home.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View view) {
				final Intent homePageIntent = new Intent(Constants.HOMEPAGE_ACTIVITY);
				startActivity(homePageIntent);
			}
		});
		
 		requests.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View v) {
				final Intent requestsIntent = new Intent(Constants.REQUESTS_ACTIVITY);
				startActivity(requestsIntent);			
				}
		});

		final ImageView EN_Selection = (ImageView) findViewById(R.id.language_selection_imageview_english);
		EN_Selection.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View view) {
				final SharedPreferences settings = getSharedPreferences(Constants.APP_NAME, 0);
				final SharedPreferences.Editor editor = settings.edit();
				editor.putString(Constants.LANGUAGE,
						Language.ENGLISH.toString());
				editor.commit();
				runAnimation(R.id.language_selection_imageview_english);
				final Intent homePageIntent = new Intent(Constants.HOMEPAGE_ACTIVITY);
				startActivity(homePageIntent);
			}
		});

		final ImageView GR_Selection = (ImageView) findViewById(R.id.language_selection_imageview_greek);
		GR_Selection.setOnClickListener(new View.OnClickListener() {
			public void onClick(final View view) {
				final SharedPreferences settings = getSharedPreferences(
						Constants.APP_NAME, 0);
				final SharedPreferences.Editor editor = settings.edit();
				editor.putString(Constants.LANGUAGE, Language.GREEK.toString());
				editor.commit();
				runAnimation(R.id.language_selection_imageview_greek);
				final Intent homePageIntent = new Intent(Constants.HOMEPAGE_ACTIVITY);
				startActivity(homePageIntent);
			}
		});
	}

	private void runAnimation(final int id_sel) {
		final Animation lang_selected = AnimationUtils.loadAnimation(this,
				R.anim.language_scale_down);
		final ImageView iv1 = (ImageView) findViewById(id_sel);
		iv1.startAnimation(lang_selected);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (!firstrun) {
			try {
				int counter = RequestManager.getInstance().getRequestCount();
				this.counter.setText(""+counter);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    // manage message counter
		    if (this.counter.getText().equals("0")) 
		        this.counter.setVisibility(View.INVISIBLE);
		     else
		        this.counter.setVisibility(View.VISIBLE);
		}
	}
}
